package pl.oczadly.jabberclient.addcontact.view;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JTextField;

import pl.oczadly.jabberclient.addcontact.presenter.AddContactPresenter;
import pl.oczadly.jabberclient.addcontact.presenter.IAddContactPresenter;
import pl.oczadly.jabberclient.mainwindow.model.MyListModel;
import pl.oczadly.jabberclient.utils.Utils;

public class AddContactView implements IAddContactView {

	private JFrame frame;
	private JTextField nameField;
	private JTextField idField;
	
	private JList list;
	private MyListModel listModel = MyListModel.getInstance();
	
	
	public AddContactView( JList list ) {
		this.list = list;
	}
	
	public void createUI() {
		frame = new JFrame( Utils.ADD_CONTACT );
		
		addLayout( frame );
		frame.pack();
		
		frame.setSize( 250, 120 );
		frame.setDefaultCloseOperation( JFrame.DISPOSE_ON_CLOSE );
		frame.setVisible( true );
	}

	public void close() {
		frame.dispose();
	}
	
	private void addLayout( JFrame frame ) {
		GridBagLayout layout = new GridBagLayout();
		frame.getContentPane().setLayout( layout );
		GridBagConstraints c = new GridBagConstraints();

		JLabel loginLabel = new JLabel( Utils.LOGIN_LABEL );
		c.gridx = 0;
		c.gridy = 0;
		c.anchor = GridBagConstraints.FIRST_LINE_START;
		frame.add( loginLabel, c );
		
		idField = new JTextField( 20 );
		c.gridx = 1;
		c.anchor = GridBagConstraints.PAGE_START;
		frame.add( idField, c );
		
		JLabel nameLabel = new JLabel( Utils.NAME );
		c.gridx = 0;
		c.gridy = 1;
		c.anchor = GridBagConstraints.LINE_START;
		frame.add( nameLabel, c );
		
		nameField = new JTextField( 20 );
		nameField.addActionListener( new AddContactListener() );
		c.gridx = 1;
		c.anchor = GridBagConstraints.CENTER;
		frame.add( nameField, c );
		
		JPanel buttonPanel = new JPanel();
		c.gridy = 2;
		c.anchor = GridBagConstraints.PAGE_END;
		
		JButton addButton = new JButton( Utils.ADD_CONTACT );
		addButton.addActionListener( new AddContactListener() );
		buttonPanel.add( addButton );
		
		JButton cancelButton = new JButton( Utils.CANCEL );
		cancelButton.addActionListener( new CancelListener() );
		buttonPanel.add( cancelButton );
		
		frame.add( buttonPanel, c );
	}
	
	private class AddContactListener implements ActionListener {

		public void actionPerformed( ActionEvent e ) {
			// TODO: walidacja loginu i hasla
			String id = idField.getText();
			String name = nameField.getText();
			
			IAddContactPresenter presenter = new AddContactPresenter( id, name );
			presenter.addContact();
			
			listModel.add( id, name );
			list.setListData( listModel.getAll() );
			close();
		}
		
	}
	
	private class CancelListener implements ActionListener {

		public void actionPerformed( ActionEvent e ) {
			close();
		}
		
	}
	
}
